package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.mapper.UserMapper;
import com.qs.pojo.dto.FeedbackDto;
import com.qs.pojo.entity.Feedback;
import com.qs.mapper.FeedbackMapper;
import com.qs.pojo.entity.User;
import com.qs.service.IFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-02
 */
@Service
@RequiredArgsConstructor
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    private final UserMapper userMapper;

    @Override
    public Map<String, Object> listFeedback(Integer pageNo, Integer pageSize) {
        Page<Feedback> pageInfo = new Page<Feedback>(pageNo, pageSize);
        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        Page<Feedback> feedbackPage = baseMapper.selectPage(pageInfo, wrapper);
        if(null != feedbackPage){
            List<Feedback> records = feedbackPage.getRecords();
            List<FeedbackDto> list = new ArrayList<>();
            if(!CollectionUtils.isEmpty(records)){
                records.stream().forEach(item -> {
                    User user = userMapper.selectById(item.getUserId());
                    FeedbackDto feedbackDto = new FeedbackDto();
                    BeanUtils.copyProperties(item,feedbackDto);
                    feedbackDto.setNickName(user.getNickName());
                    feedbackDto.setAvatar(user.getAvatar());
                    feedbackDto.setKey(item.getId());
                    list.add(feedbackDto);
                });
            }
            Map<String, Object> map = new HashMap<String, Object>(list.size());
            map.put("data",list);
            map.put("total",feedbackPage.getTotal());
            return map;
        }
        return null;
    }

    @Override
    public FeedbackDto echo(Integer id) {
        if(null != id){
            Feedback feedback = baseMapper.selectById(id);
            User user = userMapper.selectById(feedback.getUserId());
            FeedbackDto feedbackDto = new FeedbackDto();
            BeanUtils.copyProperties(feedback,feedbackDto);
            feedbackDto.setNickName(user.getNickName());
            feedbackDto.setAvatar(user.getAvatar());
            return feedbackDto;
        }
        return null;
    }

    @Override
    public void updateByKey(FeedbackDto feedbackDto) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(feedbackDto,feedback);
        baseMapper.updateById(feedback);
    }
}
